/**
 *  Copyright (c)  2010  Angel Ortiz.
 *  Permission is granted to copy, distribute and/or modify this document
 *  under the terms of the GNU Free Documentation License, Version 1.3
 *  or any later version published by the Free Software Foundation;
 *  with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
 *  A copy of the license is included in the section entitled "GNU
 *  Free Documentation License". 
 */

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.Border;
import javax.swing.table.TableColumn;

/**
 * This class is a GUI for the interactive tables of structures
 * 
 * @author angel
 * @date 02/12/2010
 */
/**
 * @author angel
 * 
 */
public class StructureOverview extends JPanel implements ActionListener {

        protected Font headerTile = new Font("timesroman", Font.BOLD, 30);
        protected Font headerSubtitle = new Font("serif", Font.ITALIC, 18);
        protected Font regularText = new Font("monospaced",
                        Font.LAYOUT_LEFT_TO_RIGHT, 14);
        protected final String[] COL_NAMES = { " Id ", " Type ", " Offensive ",
                        " Deffensive ", " Armor ", " Production ", " Health ", " Upkeep ",
                        " Player's Id ", " City's Id ", " Mission " };
        protected KeyboardListener kl = null;
        protected Player player = null;
        protected JTable table = null;
        protected TableModelObj tableModel = null;
        protected JScrollPane scrollPane = null;
        protected View view = null;

        /**
         * Default constructor
         * 
         * @param player - current player
         * @param kl - keyboard listener
         */
        public StructureOverview(Player player, KeyboardListener kl, View view) {
                super(new BorderLayout());
                this.player = player;
                this.kl = kl;
                this.view = view;
                this.addKeyListener(kl);
                loadGUI();
        }

        /**
         * Draws all GUI components to be contained in this paneled form
         */
        public void loadGUI() {

                tableModel = new TableModelObj(this.COL_NAMES, this
                                .getTableDataFromPlayer());
                table = new JTable(tableModel);
                table.setPreferredScrollableViewportSize(new Dimension(980, 96));
                table.setCellSelectionEnabled(false);
 
                
                scrollPane = new JScrollPane();
                scrollPane.setViewportView(table);
                JPanel titlesPanel = new JPanel(new GridLayout(2, 0));
                Border border = BorderFactory.createMatteBorder(0, 0, 5, 0, new Color(
                                15, 20, 95));

                JLabel titleLabel = new JLabel("Structure Overview");
                titleLabel.setFont(headerTile);
                titleLabel.setBorder(border);

                JLabel subTitleLabel = new JLabel(
                                "A summary of all game structure statistics");
                subTitleLabel.setFont(headerSubtitle);

                JPanel buttonsPanel = new JPanel(new GridLayout(2, 0));

                JButton goToMainBtn = new JButton("Go to Main Screen");
                goToMainBtn.setMnemonic(KeyEvent.VK_G);
                goToMainBtn.addActionListener(this);

                JButton goToUnitsBtn = new JButton("Go to Units Overview");
                goToUnitsBtn.setMnemonic(KeyEvent.VK_S);
                goToUnitsBtn.addActionListener(this);

                buttonsPanel.add(goToMainBtn);
                buttonsPanel.add(goToUnitsBtn);

                titlesPanel.add(titleLabel);
                titlesPanel.add(subTitleLabel);

                this.add(titlesPanel, BorderLayout.PAGE_START);
                this.add(scrollPane, BorderLayout.CENTER);
                this.add(buttonsPanel, BorderLayout.PAGE_END);

        }

        /**
         * Returns a summary of all statistics from structures in the game in and
         * object array 2D
         * 
         * @return - structures statistics
         */
        public Object[][] getTableDataFromPlayer() {

        		int count = 0;
                int[][] tempSize = player.getArrayOfSizes();
                Tileable[][][] tileables = player.getTileables();
                int size = 0;
                for (int i = 0; i < tileables[1].length; i++) {
                        size += tempSize[1][i];
                }

                Object[][] tempObj = new Object[size][this.COL_NAMES.length];

                for (int i = 0; i < tileables[1].length; i++) {
        			for (int j = 0; j < tempSize[1][i]; j++) {
        				for (int k = 0; k < this.COL_NAMES.length - 1; k++) {
        					if (k == 0) {
        						tempObj[count][k] = (tileables[1][i][j].getStatus()[0]);
        					} else if (k == 1) {
        						tempObj[count][k] = tileables[1][i][j]
        								.getTypeName(tileables[1][i][j].getStatus()[k]);
        					} else {
        						tempObj[count][k] = tileables[1][i][j].getStatus()[k];
        					}
        				}
        				tempObj[count][this.COL_NAMES.length - 1] = ((Structure) tileables[1][i][j])
        						.getCommandTxt();
        				count++;
        			}
        		}

                return tempObj;
        }
        /**
         * Controls through the action event of the buttons in the panel,
         * which panel gets focus; Overrides parent
         */
        @Override
        public void actionPerformed(ActionEvent e) {
                if (e.getActionCommand().equalsIgnoreCase("go to main screen")) {
                        this.setEnabled(false);
                        this.setFocusable(false);
                        this.setVisible(false);

                        this.enableInputMethods(false);
                        view.setFocus(0);
                } else if (e.getActionCommand()
                                .equalsIgnoreCase("Go to Units Overview")) {
                        this.setEnabled(false);
                        this.setFocusable(false);
                        this.setVisible(false);

                        this.enableInputMethods(false);
                        view.setFocus(1);
                }

        }
        /**
         * Updates data model of interactive tables with fresh data
         */
        public void refresh() {
                this.tableModel.setData(this.getTableDataFromPlayer());
                this.table.setModel(this.tableModel);
                this.scrollPane.setViewportView(this.table);
        }
}